home *** CD-ROM | disk | FTP | other *** search
- {$D-,L-,Y-}
-
- UNIT Strings1;
-
- { Description:
-
- A collection of useful PChar/STRING related functions for
- safe string handling:
-
- FUNCTION StrCopyNL(Dest,Source: PChar) : PChar;
-
- Copies Source to Dest and then appends the #13#10 chars to the
- end of the Dest.
-
- FUNCTION StrCatNL(Dest,Source: PChar): PChar;
-
- Appends Source to Dest and then appends the #13#10 chars to the
- end of the Dest.
-
- FUNCTION StrBoolean(Dest: PChar; AValue: BOOLEAN;
- TrueStr,FalseStr: PChar) : PChar;
-
- Sets 'Dest' to be a copy of either the 'TrueStr' if AValue
- os TRUE or 'FalseStr' if AValue is FALSE.
-
- FUNCTION SafeStrLen(Source: PChar) : WORD;
-
- A safe form of StrLen that returns NIL and #0 type PChars
- as zero length.
-
- FUNCTION SafeStrIComp(S1,S2: PChar) : INTEGER;
-
- A safe form of StrIComp that handles NIL and #0 type
- parameters.
-
- FUNCTION SafeStrCopy(Dest,Source: PChar) : PChar;
-
- A safe form of StrCopy that handles NIL and #0 type parameters.
-
- FUNCTION StrPadRight(Dest,Source : PChar; PadLength : INTEGER) : PChar;
-
- Copies Source to Dest and pads Dest with #32 to force a
- Dest string length of PadLength.
-
- FUNCTION StrPadLeft(Dest,Source : PChar; PadLength : INTEGER) : PChar;
-
- Copies Source to Dest and pads Dest with #32 to force a
- Dest string length of PadLength.
-
- FUNCTION StrPadCenter(Dest,Source : PChar; PadLength : INTEGER) : PChar;
-
- Copies Source to Dest and pads Dest with #32 to force a
- Dest string length of PadLength.
-
- FUNCTION StrJustify(Dest,Source : PChar;
- AJust : WORD;
- ATextLen : INTEGER) : PChar;
-
- Copies Source to Dest and justifies the text result.
-
- FUNCTION StrStrip(Dest,Source: PChar) : PChar;
-
- Copies a stripped version of Source to Dest. All leading and trailing
- blanks are removed.
-
- FUNCTION SafeStrUpper(Source: PChar) : PChar;
-
- Converts Source to upper case.
-
- FUNCTION DoubleToStr(Dest: PChar;
- ANumber: DOUBLE;
- ADecPlaces: INTEGER) : PChar;
-
- Returns ANumber converted to a PChar string with decimal places.
-
- FUNCTION RealToStr(Dest: PChar; ANumber: REAL; ADecPlaces: INTEGER) : PChar;
-
- Returns ANumber converted to a PChar string with decimal places.
-
- FUNCTION WordToStr(Dest: PChar; AWord: WORD) : PChar;
-
- Returns AWord converted to a PChar string.
-
- FUNCTION IntegerToStr(Dest: PChar; AnInteger: INTEGER) : PChar;
-
- Returns AnInteger converted to a PChar string.
-
- FUNCTION LongIntToStr(Dest: PChar; ALongInt: LONGINT) : PChar;
-
- Returns ALongInt converted to a PChar string.
-
- FUNCTION StrPasPadRight(Source: PChar; Padlength: INTEGER) : STRING;
-
- Copies and returns the Source PChar as a STRING variable
- right padded with spaces to the Padlength.
-
- }
-
- { :::::::::::::::::::::::::::::::::::::::::::::::::::::: }
-
- INTERFACE
-
- uses Strings,winTypes;
-
- { :::::::::::::::::::::::::::::::::::::::::::::::::::::: }
-
- FUNCTION StrCopyNL(Dest,Source: PChar) : PChar;
- FUNCTION StrCatNL(Dest,Source: PChar): PChar;
-
- FUNCTION StrBoolean(Dest: PChar; AValue: BOOLEAN;
- TrueStr,FalseStr: PChar) : PChar;
- FUNCTION SafeStrLen(Source: PChar) : WORD;
- FUNCTION SafeStrIComp(S1,S2: PChar) : INTEGER;
- FUNCTION SafeStrCopy(Dest,Source: PChar) : PChar;
- FUNCTION StrPadRight(Dest,Source : PChar; PadLength : INTEGER) : PChar;
- FUNCTION StrPadLeft(Dest,Source : PChar; PadLength : INTEGER) : PChar;
- FUNCTION StrPadCenter(Dest,Source : PChar; PadLength : INTEGER) : PChar;
-
- FUNCTION StrJustify(Dest,Source : PChar;
- AJust : WORD;
- ATextLen : INTEGER) : PChar;
-
- FUNCTION StrStrip(Dest,Source: PChar) : PChar;
-
- FUNCTION SafeStrUpper(Source: PChar) : PChar;
-
- FUNCTION DoubleToStr(Dest: PChar;
- ANumber: DOUBLE;
- ADecPlaces: INTEGER) : PChar;
- FUNCTION RealToStr(Dest: PChar; ANumber: REAL; ADecPlaces: INTEGER) : PChar;
- FUNCTION WordToStr(Dest: PChar; AWord: WORD) : PChar;
- FUNCTION IntegerToStr(Dest: PChar; AnInteger: INTEGER) : PChar;
- FUNCTION LongIntToStr(Dest: PChar; ALongInt: LONGINT) : PChar;
-
- FUNCTION StrPasPadRight(Source: PChar; Padlength: INTEGER) : STRING;
-
- { :::::::::::::::::::::::::::::::::::::::::::::::::::::: }
-
- IMPLEMENTATION
-
- { :::::::::::::::::::::::::::::::::::::::::::::::::::::: }
-
-
- FUNCTION StrCopyNL(Dest,Source: PChar) : PChar;
- BEGIN
- StrCopy(Dest,Source);
- StrCat(Dest,#13#10);
- StrCopyNL := Dest;
- END;
-
- { ------------------------------------------------------------- }
-
- FUNCTION StrCatNL(Dest,Source: PChar) : PChar;
- BEGIN
- StrCat(Dest,Source);
- StrCat(Dest,#13#10);
- StrCatNL := Dest;
- END;
-
- { ------------------------------------------------------------- }
-
-
- FUNCTION StrBoolean(Dest: PChar;
- AValue: BOOLEAN;
- TrueStr,FalseStr: PChar) : PChar;
- BEGIN
- IF AValue THEN
- StrCopy(Dest,TrueStr)
- ELSE
- StrCopy(Dest,FalseStr);
- StrBoolean := Dest;
- END;
-
- { ------------------------------------------------------------- }
-
- FUNCTION SafeStrLen(Source: PChar) : WORD;
- BEGIN
- IF (Source = NIL) OR (Source[0] = #0) THEN
- SafeStrLen := 0
- ELSE
- SafeStrLen := StrLen(Source)
- END;
-
- { ------------------------------------------------------------- }
-
- FUNCTION StrPasPadRight(Source: PChar; Padlength: INTEGER) : STRING;
- VAR
- AStr : STRING;
- Long,idx : INTEGER;
- BEGIN
- IF Source = NIL THEN
- BEGIN
- AStr := '';
- Long := 0;
- END
- ELSE
- BEGIN
- AStr := StrPas(Source);
- Long := StrLen(Source);
- END;
- IF Long > PadLength THEN
- StrPasPadRight := COPY(AStr,1,PadLength)
- ELSE
- BEGIN
- IF Long = PadLength THEN
- StrPasPadRight := AStr
- ELSE
- BEGIN
- FOR idx := 1 TO (PadLength-Long) DO
- AStr := AStr + ' ';
- StrPasPadRight := AStr;
- END
- END
- END;
-
- { ------------------------------------------------------------- }
-
- FUNCTION SafeStrIComp(S1,S2: PChar) : INTEGER;
- BEGIN
- IF (S1=NIL) OR (S2=NIL) THEN
- BEGIN
- IF S1 < S2 THEN
- BEGIN
- IF StrLen(S2) = 0 THEN
- SafeStrIComp := 0
- ELSE
- SafeStrIComp := -1;
- END
- ELSE
- BEGIN
- IF S1 = S2 THEN
- SafeStrIComp := 0
- ELSE
- BEGIN
- IF StrLen(S1) = 0 THEN
- SafeStrIComp := 0
- ELSE
- SafeStrIComp := 1
- END
- END
- END
- ELSE
- SafeStrIComp := StrIComp(S1,S2);
- END;
-
- { ----------------------------------------- }
-
- FUNCTION SafeStrCopy(Dest,Source: PChar) : PChar;
- BEGIN
- IF Source = NIL THEN
- StrCopy(Dest,#0)
- ELSE
- BEGIN
- IF StrLen(Source) = 0 THEN
- StrCopy(Dest,#0)
- ELSE
- StrCopy(Dest,Source);
- END;
- SafeStrCopy := Dest;
- END;
-
- { ----------------------------------------- }
-
- FUNCTION SafeStrUpper(Source: PChar) : PChar;
- BEGIN
- IF SafeStrLen(Source) > 0 THEN
- SafeStrUpper := StrUpper(Source)
- ELSE
- SafeStrUpper := Source;
- END;
-
- { ----------------------------------------- }
-
- FUNCTION StrPadRight(Dest,Source : PChar; PadLength : INTEGER) : PChar;
- VAR
- Long,C : INTEGER;
- BEGIN
- Long := StrLen(Source);
- IF Long >= PadLength THEN
- StrCopy(Dest,Source)
- ELSE
- BEGIN
- StrCopy(Dest,Source);
- FOR C := 1 TO (PadLength-Long) DO
- StrCat(Dest,' ');
- END;
- StrPadRight := Dest;
- END;
-
- { -------------------------------------------- }
-
- FUNCTION StrPadLeft(Dest,Source : PChar; PadLength : INTEGER) : PChar;
- VAR
- Long,C : INTEGER;
- BEGIN
- Long := StrLen(Source);
- IF Long >= PadLength THEN
- StrLCopy(Dest,Source,PadLength)
- ELSE
- BEGIN
- StrCopy(Dest,' ');
- IF (Padlength-Long) > 1 THEN
- BEGIN
- FOR C := 2 TO (PadLength-Long) DO
- StrCat(Dest,' ');
- END;
- StrCat(Dest,Source);
- END;
- StrPadLeft := Dest;
- END;
-
- { -------------------------------------------- }
-
- FUNCTION StrPadCenter(Dest,Source : PChar; PadLength : INTEGER) : PChar;
- VAR
- m,Long,C : INTEGER;
- BEGIN
- Long := StrLen(Source);
- IF Long >= PadLength THEN
- StrLCopy(Dest,Source,PadLength)
- ELSE
- BEGIN
- m := (PadLength-Long) DIV 2;
- IF M > 0 THEN
- BEGIN
- StrCopy(Dest,' ');
- IF m > 1 THEN
- BEGIN
- FOR C := 2 TO m DO
- StrCat(Dest,' ');
- END
- END;
- StrCat(Dest,Source);
- END;
- StrPadCenter := Dest;
- END;
-
- { -------------------------------------------- }
-
- FUNCTION StrJustify(Dest,Source : PChar;
- AJust : WORD;
- ATextLen : INTEGER) : PChar;
- VAR
- Long : INTEGER;
- BEGIN
- Long := StrLen(Source);
- IF Long > 0 THEN
- BEGIN
- CASE AJust OF
- es_Left : StrPadRight(Dest,Source,ATextLen);
- es_Right : StrPadLeft(Dest,Source,ATextLen);
- es_Center : StrPadCenter(Dest,Source,ATextLen);
- END
- END;
- StrJustify := Dest;
- END;
-
- { ------------------------------------------------ }
-
- FUNCTION StrStrip(Dest,Source: PChar) : PChar;
- VAR
- S,E : INTEGER;
- BEGIN
- S := 0;
- E := SafeStrLen(Source)-1;
- IF E > 0 THEN
- BEGIN
- WHILE Source[S] = ' ' DO
- INC(S);
- WHILE Source[E] = ' ' DO
- DEC(E);
- StrLCopy(Dest,@Source[S],E-S+1);
- END;
- StrStrip := Dest;
- END;
-
- { ------------------------------------------------ }
-
- FUNCTION RealToStr(Dest: PChar; ANumber: REAL; ADecPlaces: INTEGER) : PChar;
- VAR
- AStr : ARRAY[0..80] OF CHAR;
- BEGIN
- STR(ANumber:40:ADecPlaces,AStr);
- StrStrip(Dest,AStr);
- RealToStr := Dest;
- END;
-
- { ------------------------------------------------ }
-
- FUNCTION DoubleToStr(Dest : PChar;
- ANumber : DOUBLE;
- ADecPlaces : INTEGER) : PChar;
- VAR
- AStr : ARRAY[0..80] OF CHAR;
- BEGIN
- STR(ANumber:40:ADecPlaces,AStr);
- StrStrip(Dest,AStr);
- DoubleToStr := Dest;
- END;
-
- { ---------------------------------------- }
-
- FUNCTION WordToStr(Dest: PChar; AWord: WORD) : PChar;
- VAR
- AStr : ARRAY[0..20] OF CHAR;
- BEGIN
- Str(AWord,AStr);
- StrCopy(Dest,AStr);
- WordToStr := Dest;
- END;
-
- { ---------------------------------------- }
-
- FUNCTION IntegerToStr(Dest: PChar; AnInteger: INTEGER) : PChar;
- VAR
- AStr : ARRAY[0..20] OF CHAR;
- BEGIN
- STR(AnInteger,AStr);
- SafeStrCopy(Dest,AStr);
- IntegerToStr := Dest;
- END;
-
- { ---------------------------------------- }
-
- FUNCTION LongIntToStr(Dest: PChar; ALongInt: LONGINT) : PChar;
- VAR
- AStr : ARRAY[0..20] OF CHAR;
- BEGIN
- Str(ALongInt,AStr);
- StrCopy(Dest,AStr);
- LongIntToStr := Dest;
- END;
-
- { :::::::::::::::::::::::::::::::::::::::::::::::::::::: }
-
- BEGIN
-
- END.